<?php
require_once(dirname(__FILE__) . '/app.php');


$now = time();
$detail = array();



/* zhangrui add start --根据城市查出所有团购 */
$city_id = abs(intval($city['id']));

$now = time();
$size = abs(intval($INI['system']['indexteam']));
if ($size<=1) return current_team($city_id);
$condition = array( 
	'team_type' => 'normal',
	"begin_time < '{$now}'",
	"end_time > '{$now}'",
	'group_id' => 40,//代表的是“酒店旅游”类别
	'audit_status' => 0,
	);
$sub_id = abs(intval($_GET['sid']));
$area_id = abs(intval($_GET['aid']));
$orderby = 'ORDER BY `sort_order` DESC, `id` DESC';
if('price' == strval($_GET['order'])){$orderby = 'ORDER BY `team_price` DESC';}
if('number' == strval($_GET['order'])){$orderby = 'ORDER BY `now_number` DESC';}
if('new' == strval($_GET['order'])){$orderby = 'ORDER BY `begin_time` DESC';}
if($sub_id) $condition['sub_id']=$sub_id;
if($area_id) $condition['area_id']=$area_id;
if($_GET['keyword'] && '请输入商品名称' != $_GET['keyword']){
	$condition[] = "title like '%".mysql_escape_string($_GET['keyword'])."%'";
}
$condition[] = "(city_ids like '%@{$city_id}@%' or city_ids like '%@0@%') or (city_ids = '' and city_id in(0,{$city_id}))";
$count = Table::Count('team', $condition);
list($pagesize, $offset, $pagestring) = pagestring($count, $size);
$teams = DB::LimitQuery('team', array(
	'condition' => $condition,
	'order' => $orderby,
	'size' => $pagesize,
	'offset' => $offset,
	));
/* zhangrui add end */

if (!$teams) { redirect( WEB_ROOT . '/team/index.php'); }

foreach($teams AS $index => $team) {

	if($team['end_time']<$team['begin_time']){$team['end_time']=$team['begin_time'];}
	$diff_time = $left_time = $team['end_time']-$now;
	if ( $team['team_type'] == 'seconds' && $team['begin_time'] >= $now ) {
		$diff_time = $left_time = $team['begin_time']-$now;
	}

	/* progress bar size */
	$detail[$team['id']]['bar_size'] = ceil(190*($team['now_number']/$team['min_number']));
	$detail[$team['id']]['bar_offset'] = ceil(5*($team['now_number']/$team['min_number']));

	$left_day = floor($diff_time/86400);
	$left_time = $left_time % 86400;
	$left_hour = floor($left_time/3600);
	$left_time = $left_time % 3600;
	$left_minute = floor($left_time/60);
	$left_time = $left_time % 60;

	$detail[$team['id']]['diff_time'] = $diff_time;
	$detail[$team['id']]['left_day'] = $left_day;
	$detail[$team['id']]['left_hour'] = $left_hour;
	$detail[$team['id']]['left_minute'] = $left_minute;
	$detail[$team['id']]['left_time'] = $left_time;
	$detail[$team['id']]['is_today'] = $team['begin_time'] + 3600*24 > time() ? 1:0;

	/* state */
	$team['state'] = team_state($team);
	$teams[$index] = $team;
}
$team = null;
include template('team_jdly');
